package com.eric.service.impl;

import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.eric.domain.dto.ProfessionalCategoryDTO;
import com.eric.domain.po.ProfessionalCategory;
import com.eric.domain.vo.ProfessionalCategoryVO;
import com.eric.mapper.ProfessionalCategoryMapper;
import com.eric.service.ProfessionalCategoryService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

import static com.eric.domain.po.table.ProfessionalCategoryTableDef.PROFESSIONAL_CATEGORY;


@Service
public class ProfessionalCategoryServiceImpl extends ServiceImpl<ProfessionalCategoryMapper, ProfessionalCategory>  implements ProfessionalCategoryService{

    @Override
    public SaResult saveOneService(ProfessionalCategoryVO professionalCategoryVO) {
        ProfessionalCategory one = queryChain().select()
                .where(PROFESSIONAL_CATEGORY.PROFESSIONAL_NAME.eq(professionalCategoryVO.getProfessionalName()))
                .one();
        if (BeanUtil.isEmpty(one)){
            ProfessionalCategory professionalCategory=new ProfessionalCategory();
            BeanUtil.copyProperties(professionalCategoryVO,professionalCategory);
            boolean save = save(professionalCategory);
            return save ? SaResult.ok() : (SaResult.error("录入错误"));
        }else {
            return SaResult.error("项目已经存在！");
        }
    }

    @Override
    public SaResult updateByIdService(ProfessionalCategoryDTO professionalCategoryDTO) {
        boolean nameEffective=false;
        boolean descriptionEffective =false;
        if (!StrUtil.isBlank(professionalCategoryDTO.getProfessionalName())){
            nameEffective=true;
        }
        if (!StrUtil.isBlank(professionalCategoryDTO.getDescription())){
            descriptionEffective=true;
        }
        ProfessionalCategory professionalCategory = queryChain()
                .where(PROFESSIONAL_CATEGORY.PROFESSIONAL_NAME
                        .eq(StrUtil.trim(professionalCategoryDTO.getProfessionalName())))
                .one();
        if (!BeanUtil.isEmpty(professionalCategory)&& !Objects.equals(professionalCategory.getId(),professionalCategoryDTO.getId())){
            return SaResult.error("专业名称已经存在！");
        }
        boolean updated = UpdateChain.of(ProfessionalCategory.class)
                .set(PROFESSIONAL_CATEGORY.PROFESSIONAL_NAME, professionalCategoryDTO.getProfessionalName(), nameEffective)
                .set(PROFESSIONAL_CATEGORY.DESCRIPTION, professionalCategoryDTO.getDescription(), descriptionEffective)
                .where(PROFESSIONAL_CATEGORY.ID.eq(professionalCategoryDTO.getId()))
                .update();
        return SaResult.data(updated);
    }

    @Override
    public SaResult getListByPageService(Integer cur, Integer pageSize) {
        if (cur==null||cur<=0){
            cur=1;
        }
        if (pageSize==null||pageSize<=0){
            pageSize=11;
        }
        List<ProfessionalCategory> professionalCategoryList = queryChain().select()
                .from(PROFESSIONAL_CATEGORY).orderBy(PROFESSIONAL_CATEGORY.UPDATE_TIME.desc())
                .page(new Page<>(cur, pageSize)).getRecords();
        if (!professionalCategoryList.isEmpty()){
            List<ProfessionalCategoryDTO> professionalCategoryDTOList = BeanUtil.copyToList(professionalCategoryList, ProfessionalCategoryDTO.class);
            return SaResult.data(professionalCategoryDTOList);
        }

        return SaResult.error("没有数据");
    }

    @Override
    public SaResult getListByNameService(String name, Integer cur, Integer pageSize) {
        if (cur==null||cur<=0){
            cur=1;
        }
        if (pageSize==null||pageSize<=0){
            pageSize=11;
        }
        List<ProfessionalCategory> professionalCategoryList = queryChain().select()
                .where(PROFESSIONAL_CATEGORY.PROFESSIONAL_NAME.like(name, true))
                .orderBy(PROFESSIONAL_CATEGORY.UPDATE_TIME.desc())
                .page(new Page<>(cur, pageSize)).getRecords();

        if (!professionalCategoryList.isEmpty()){
            List<ProfessionalCategoryDTO> professionalCategoryDTOList = BeanUtil.copyToList(professionalCategoryList, ProfessionalCategoryDTO.class);
            return SaResult.data(professionalCategoryDTOList);
        }
        return SaResult.error("没有数据！");
    }

    @Override
    public SaResult getListByNameCountService(String name) {
        List<ProfessionalCategory> professionalCategoryList = queryChain()
                .select()
                .where(PROFESSIONAL_CATEGORY.PROFESSIONAL_NAME.like(name, true)).list();
        int size = professionalCategoryList.size();
        return SaResult.data(size);
    }
}
